library(dplyr)
library(magrittr)
library(tidyr)
library(ggplot2)

dat <- rio::import("data/Demo_book_country_3.dta")


dem2 <- dat %>%
  filter(year >= 1789, year <= 2019, europe_harbors_def == 0) %>%
  select(country_id, year, v2x_polyarchy_imp_100, chrstprotpct, eur_lang_pct, durbritain_new, 
         eur_pct_est_smooth, lp_lat_abst)


r2_regs_df <- data.frame(start_year = 1789:1990,
                         end_year = 1818:2019,
                         coef = NA,
                         se = NA,
                         t_value = NA,
                         N = NA,
                         r2 = NA, 
                         series = "lexical")

for (i in 1:nrow(r2_regs_df)){
  subset_data <- dem2[which(dem2$year %in% c(r2_regs_df$start_year[i]:r2_regs_df$end_year[i])), ]
  reg <- miceadds::lm.cluster(v2x_polyarchy_imp_100 ~ chrstprotpct + eur_pct_est_smooth + durbritain_new,
                              data = subset_data, cluster = "country_id")
  r2_regs_df$r2[i] <- summary(reg$lm_res)$r.squared
}


g <- r2_regs_df %>%
  mutate(year = end_year - 15) %>%
  ggplot(aes(x = year)) +
  geom_point(aes(y = r2)) +
  scale_x_continuous(breaks = seq(1800, 2000, by = 25)) +
  ylim(0, 1) + 
  labs(x = "Year", y = latex2exp::TeX("$R^2$")) +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(color = "black"))

# ggsave(plot = g, filename = "figure_13_1.png")
ggsave(plot = g, filename = "output/figure_13_1.tiff", dpi = 300)

